package com.aat.dxfy.jdbc.datasource;

import java.sql.Connection;

import org.logicalcobwebs.proxool.ProxoolDataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class TestMain {
	private static String driver="com.mysql.jdbc.Driver";
	private static String jdbcUrl="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";
	private static String user="root";
	private static String passwd="root";
	
	private static int count=10000;

	public static void main(String[] args) throws Exception {
		long l1=System.currentTimeMillis();
		testC3p0();
		long l2=System.currentTimeMillis();
		System.out.println("c3p0:"+(l2-l1));
		
		long l3=System.currentTimeMillis();
		testDruid();
		long l4=System.currentTimeMillis();
		System.out.println("druid:"+(l4-l3));
		
		long l5=System.currentTimeMillis();
		testProxool();
		long l6=System.currentTimeMillis();
		System.out.println("proxool:"+(l6-l5));
	}

	public static void testC3p0() throws Exception {
		ComboPooledDataSource dataS = new ComboPooledDataSource();
		// config
		dataS.setDriverClass(driver);
		dataS.setJdbcUrl(jdbcUrl);
		dataS.setUser(user);
		dataS.setPassword(passwd);
		dataS.setMaxPoolSize(100);
		dataS.setMinPoolSize(1);
		
		// action
		Connection conn = dataS.getConnection();
		int i = 0;
		while (i < count) {
			i++;
			TestDAO.query(conn);
		}
		dataS.close();
	}
	
	public static void testDruid() throws Exception {
		DruidDataSource dataS=new DruidDataSource();
		// config
		dataS.setDriverClassName(driver);
		dataS.setUrl(jdbcUrl);
		dataS.setUsername(user);
		dataS.setPassword(passwd);
		dataS.setMaxActive(100);
		dataS.setMinIdle(1);
		dataS.setInitialSize(1);
		
		//action
		Connection conn = dataS.getConnection();
		int i = 0;
		while (i < count) {
			i++;
			TestDAO.query(conn);
		}
		dataS.close();
	}
	
	public static void testProxool() throws Exception {
		ProxoolDataSource dataS=new ProxoolDataSource();
		// config
		dataS.setDriver(driver);
		dataS.setDriverUrl(jdbcUrl);
		dataS.setUser(user);
		dataS.setPassword(passwd);
		dataS.setMaximumConnectionCount(100);
		dataS.setMinimumConnectionCount(1);
		
		//action
		Connection conn = dataS.getConnection();
		int i = 0;
		while (i < count) {
			i++;
			TestDAO.query(conn);
		}
	}
	
	
}
